require 'config/boot'
require 'config/environment'
require 'lib/simple-json'

cashe_size = ENV['cashe_size'].nil? ? 1000 : ENV['cashe_size']

#実行する際はシリアライズをコメントアウト
task :erase do
  size = Corpus.count
  limit = cashe_size
  repeat_times = (size / limit) + 1
  repeat_times.times{|i|
    cashe = Array.new
    p "erasing at #{i}/#{repeat_times}"
    Corpus.find(:all,
      :limit => limit,
      :offset => limit * i
    ).each{|sentence|
      cashe << sentence.json
      sentence.json = nil
      sentence.save
    }
    File::open("backup","a"){|file|
      file.puts(cashe)
    }
  }
end

task :re_regist do
  parser = JsonParser.new
  size = Corpus.count
  limit = cashe_size
  repeat_times = (size / limit) + 1
  File::open("backup","r"){|file|
    repeat_times.times{|i|
      p "re-regist at #{i}/#{repeat_times}"
      Corpus.find(:all,
        :limit => limit,
        :offset => limit * i
      ).each{|sentence|
        cashe = file.gets.chomp!
        sentence.json = cashe
        sentence.save
      }
    }
  }
end